<template>
  <div class="app-container">
    <div class="default-box">
      <div class="filter-container">
        <el-button class="filter-item" type="primary" icon="el-icon-plus" round @click="createDialog = true;isadd = true">
          新增
        </el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-notebook-2" round plain @click="logDialog = true">
          日志
        </el-button>
      </div>
      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        fit
        highlight-current-row
        style="width: 100%;"
      >
        <el-table-column label="标题" min-width="180">
          <template>
            <span>123123</span>
          </template>
        </el-table-column>
        <el-table-column label="备忘" width="180">
          <template>
            <span>123123</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="200">
          <template>
            <el-switch v-model="status" active-text="启用" inactive-text="禁用" />
          </template>
        </el-table-column>
        <el-table-column label="是否弹窗" width="200">
          <template>
            <el-switch v-model="status" active-text="启用" inactive-text="禁用" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="200">
          <template>
            <span>
              2023-03-15 17:25:28
            </span>
          </template>
        </el-table-column>
        <el-table-column label="过期时间" width="200">
          <template>
            <span>
              2023-03-15 17:25:28
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" class-name="small-padding fixed-width">
          <template>
            <i class="el-icon-edit" :style="{color:$store.state.settings.theme}" @click="createDialog = true;isadd = false" />
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <!-- 展示新增、编辑对话框 -->
    <el-dialog
      :title="isadd?'新增公告':'编辑公告'"
      :visible.sync="createDialog"
      width="50%"
      custom-class="operate-dialog"
      top="10vh"
      center
    >
      <el-form :model="oprateform" label-width="120px">
        <el-form-item label="标题">
          <el-input v-model="oprateform.name" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="唯一标识">
          <el-input v-model="oprateform.name" placeholder="请输入唯一标识" />
        </el-form-item>
        <el-form-item label="网站LOGO">
          <el-upload
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            list-type="picture"
          >
            <el-button type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="过期时间">
          <el-date-picker
            v-model="oprateform.date"
            type="date"
            placeholder="请选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="是否弹窗公告">
          <el-switch v-model="oprateform.value" active-text="弹窗" inactive-text="普通" />
        </el-form-item>
        <el-form-item label="内容">
          <tinymce v-model="content" menubar="false" :height="300" />
        </el-form-item>
        <el-form-item label="备忘">
          <el-input v-model="oprateform.describe" type="textarea" :autosize="{ minRows: 5, maxRows: 5}" placeholder="请输入备忘" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleResetOperate">重置</el-button>
        <el-button type="primary" @click="handleOperate">提交</el-button>
      </span>
    </el-dialog>
    <!--  日志对话框 -->
    <el-dialog
      title="日志"
      :visible.sync="logDialog"
      width="80%"
      custom-class="operate-dialog main-bagcolor"
      top="10vh"
      center
    >
      <NoticeLog />
    </el-dialog>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import NoticeLog from './components/notice-log'
import { fetchList } from '@/api/article'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'Adminrole',
  components: { Pagination, Tinymce, NoticeLog },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        title: ''
      },
      status: true,
      isadd: true, // 是新增 否编辑
      createDialog: false, // 是否展示新增、编辑对话框
      logDialog: false, // 是否展示日志对话框
      oprateform: { // 新增、编辑条件
        name: '',
        describe: '',
        value: false,
        date: ''
      },
      content: ``, // 富文本内容
      fileList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    // 新增、编辑
    handleOperate() {
      this.createDialog = false
    },
    // 重置新增，编辑内容
    handleResetOperate() {
      this.oprateform = {
        name: '',
        describe: ''
      }
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    }
  }
}
</script>
<style scoped>
  [class^=el-icon-], [class*=" el-icon-"]{
    font-size: 16px;
  }
  .small-padding i{
    margin-right: 12px;
  }
  .small-padding i:last-child{
    margin-right: 0px;
  }
</style>
